﻿$(function() {
	/**
	 * 设备能耗
	 * */
	var complexpie = echarts.init(document.getElementById('complexpie'));
	optionpie = {
	        tooltip : {
	            trigger: 'item',
	            formatter: function (params, ticket, callback) {
	                if(params.seriesName=="设备状态"){
	                	return params.seriesName+" <br/>"+params.name+" : "+params.value+"分钟 "+"("+params.percent+"%)";
	                }else{
	                	return params.seriesName+" <br/>"+params.name+" : "+params.value+"度 "+"("+params.percent+"%)";
	                }
	            }
	        },
	        legend: {
	        	orient:'vertical',
	        	left:'center',
	        	top:'middle',
	            data: []
	        },
	        backgroundColor:'#e0e0e0',
	        color:['#ff6264','#61c0de','#00b19d','#f7aa47','#999933','#0084c5','#ff8463','#9bca62','#fad75f','#003366'],
	        series : [{
	        	name:'设备状态',
	            type:'pie',
	            radius:'60%',
	            center:['25%', '45%'],
	            data:[],
	            label:{
	                normal: {
	                	show:false,
		                 formatter: '{b}\n{c}分钟'
		            }
	            }
	        },{
	        	name:'能耗状态',
	            type: 'pie',
	            radius : '60%',
	            center : ['75%', '45%'],
	            data:[],
	            label: {
	                normal: {
	                	show:false,
		                formatter: '{b}\n{c}分钟'
		            }
	            }
	        }]
	    };
	complexpie.setOption(optionpie);
	
	/**
	 * 状态时长
	 * */
    var complexbar1 = echarts.init(document.getElementById('complexbar1'));
    optionbar1 = {
    	    tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        }
    	    },
    	    grid: {
                top: '10%',
                left: '4%',
                right: '4%',
                bottom: '20%',
                containLabel: true
            },
    	    legend: {
    	    	bottom: '30',
    	        data: []
    	    },
    	    backgroundColor:'#e0e0e0',
    	    color:['#ff6264','#61c0de','#00b19d','#f7aa47','#999933','#0084c5','#ff8463','#9bca62','#fad75f','#003366'],
    	    xAxis:  {
            	type: 'category',
    	        data: [],
    	        splitLine:{
	            	show:false
	            }
    	    },
    	    yAxis: {
    	    	 type: 'value',
    	    	 name:'状态时长',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
    	    	 splitLine:{
 	            	show:false
 	            }
    	    },
    	    dataZoom:{
    	    	type:'slider',
    	    	start:0,
    	    	end:100,
    	    	bottom:1
    	    },
    	    series: []
    	};
    complexbar1.setOption(optionbar1);
    
    /**
     * 满载率
     * */
    
    var complexbar2 = echarts.init(document.getElementById('complexbar2'));
	optionBar2 = {
		    tooltip: {
		        trigger: 'axis',
		        axisPointer: {
		            type: 'shadow'
		        }
		    },
		    backgroundColor:'#e0e0e0',
		    color:['#f7aa47'],
		    grid: {
	            top: '10%',
	            left: '4%',
	            right: '4%',
	            bottom: '20%',
	            containLabel: true
	        },
	        legend: {
    	    	bottom: '30',
    	        data:['满载率']
    	    },
		    xAxis: {
		        type: 'value',
		        boundaryGap: [0, 0.01],
		        nameLocation:'end',
			        axisLabel : {
	                formatter: '{value}'
	            },
	            splitLine:{
	            	show:false
	            }
		    },
		    yAxis: {
		        type: 'category',
		        name : '满载率',
		        data:[], 
	            splitLine:{
	            	show:false
	            }
		    },
		    series:[{
		    	name:'满载率',
		    	type:'bar',
		    	label:{
		    		normal:{
		    			show:true,
		    			formatter: '{c}%',
		    			position: 'right'
		    		}
		    	},
		    	data:[]
		    }]
		};
	complexbar2.setOption(optionBar2);
	
	 var lineId=0;var equId=0;
	    var	startTime="";
	    var	endTime="";
	    
	    /**
	     * slider滑动事件
	     * */
	    $('#ss').slider({
	    	range: true,
	        onComplete:function(value){//滑块值改变时触发
		       	startTime=moment(value[0]).format('YYYY-MM-DD');
		       	endTime=moment(value[1]).format('YYYY-MM-DD');
		       	getData();
	       }
	    });
	    /**
	     * 初始化
	     * */
	    var vm = new Vue({
		    el: '#itemData',
		    data:{item:[]}
		}); 

	    var timess=[parseInt(moment().subtract(7, 'days').format("x")),parseInt(moment().format("x"))];
	    $('#ss').slider('setValues',timess);
	    var arr=$('#ss').slider('getValues');
	    var time1=arr[0];
	    var time2=arr[1];
	    startTime=moment(time1).format("YYYY-MM-DD");
	    endTime=moment(time2).format("YYYY-MM-DD");
	    getData();
	    function getData(){
	    	$.post(basePath+"/single/singleComplex/startrate.json",{"lineId":lineId,"equId":equId,startTime:startTime,endTime:endTime},function(res){
	    		vm.$set('item',res);
	    		if(res.success){
	    			complexpie.setOption({
	    				legend:{
	    					data:res.pieLegend
	    				},
	    				series:[{
	    					name:'设备状态',
	    					data:res.pieSValue
	    				},{
	    					name:'能耗状态',
	    					data:res.pieCValue
	    				}]
	    			});
	    			var series=[];
	    		    for(var i = 0;i<res.eachDayValue.length;i++){
	    		        series.push({
	    		            name: res.eachDayValue[i].name,
	    		            stack: '总量',
	    		            type: 'bar',
	    		            barMaxWidth:40,
	    		            data: res.eachDayValue[i].value
	    		        });
	    		    }
	    		    complexbar1.setOption({
	    				legend: {
	    		            data: res.pieLegend
	    		        },
	    				xAxis: {
				            data: res.days
				        },
				        series:series
	    			});
	    		    complexbar2.setOption({
	    		    	yAxis: {
	    			        data:res.shiftList
	    			    },
	    		        series: [{
				            data: res.rates
				        }]
	    			});
	    		}else{
	    			alert(res.msg);
	    		}
	    	});
	    }
	    
	    /**
	     * 自定义时间按钮的点击事件
	     * */
		 $("#defineTime").click(function (){
			 var mePanel = $('#defineTimeWin');
		   	 mePanel.dialog('open');
		 });
		 
		 /**
		  * 切换设备的点击事件
		  * */
		var mePanel = $('#switchEquip');
		$("#linetree").tree({
			onClick: function(node) {
				if (node.url) {
					lineId=node.id;
					var lineGrid = $('#lines_data');
					lineGrid.datagrid('load',{
						lineId:node.id
					});
					lineGrid.datagrid('getPanel').panel('setTitle',node.text);
				}
			}
		});
		$('#lines_data').datagrid({
	 		//双击设备时，根据选中设备刷新
	 		onDblClickRow: function(rowIndex, rowData){
	 			lineId=rowData.lineId;
	 			equId=rowData.id;
	 			var arr=$('#ss').slider('getValues');
	 		    var time1=arr[0];
	 		    var time2=arr[1];
	 		    startTime=moment(time1).format("YYYY-MM-DD");
	 		    endTime=moment(time2).format("YYYY-MM-DD");
	 		    getData();
	 		    mePanel.dialog('close');
	 		}
		});
	   $("#switchEquipOK").click(function(){
	   	var row = $('#lines_data').datagrid('getSelected');
	   	if(row){
	   		lineId=row.lineId;
	   		equId=row.id;
	   		var arr=$('#ss').slider('getValues');
	   	    var time1=arr[0];
	   	    var time2=arr[1];
	   	    startTime=moment(time1).format("YYYY-MM-DD");
	   	    endTime=moment(time2).format("YYYY-MM-DD");
	   	    getData();
			$('#switchEquip').dialog('close');
	   	}else{
	   		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择一条设备记录!',timeout:3000});
	   	}
	   });
	
});